<html>
<head><meta charset="utf-8"><title>struct attribute that implements Trait · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html">struct attribute that implements Trait</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="212715965"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212715965" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dave E <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212715965">(Oct 08 2020 at 16:30)</a>:</h4>
<p>I'm just starting to learn rust for the first time (from ruby)<br>
I cant work out how to say that a Type implements a Trait.</p>
<p>I'm using rust:1.46</p>
<p>Below is an extract of my code, </p>
<div class="codehilite"><pre><span></span><code>trait Format {
    fn format(&amp;self, payload: &amp;Payload) -&gt; String;
}
struct StdoutWriter {
    formatter: [Something that implements Format trait],
}
impl StdoutWriter {
    fn write(&amp;self, payload: &amp;Payload) {
        eprintln!(&quot;{}&quot;, self.formatter.format(&amp;payload));
    }
}
</code></pre></div>


<p>My understanding is in the future I could use <code>type Formatter impl Format</code> however how can I do this on stable?</p>



<a name="212716113"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212716113" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212716113">(Oct 08 2020 at 16:31)</a>:</h4>
<p>please post this in <a class="stream" data-stream-id="122651" href="/#narrow/stream/122651-general">#general</a>, this channel is for working on the compiler itself</p>



<a name="212716187"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212716187" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212716187">(Oct 08 2020 at 16:32)</a>:</h4>
<p>(but you probably want <code>Box&lt;dyn Format&gt;</code> or <code>struct StdoutWriter&lt;T: Format&gt;</code>)</p>



<a name="212716330"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212716330" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Notification Bot <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212716330">(Oct 08 2020 at 16:32)</a>:</h4>
<p>This topic was moved here from <a class="stream-topic" data-stream-id="182449" href="/#narrow/stream/182449-t-compiler.2Fhelp/topic/struct.20attribute.20that.20implements.20Trait">#t-compiler/help &gt; struct attribute that implements Trait</a> by <span class="user-mention silent" data-user-id="116107">davidtwco</span></p>



<a name="212718136"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212718136" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dave E <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212718136">(Oct 08 2020 at 16:47)</a>:</h4>
<p><span class="user-mention" data-user-id="232545">@Joshua Nelson</span> Thanks,  Can you give me an example of what my code snippit might look like  if I was using <code>struct StdoutWriter&lt;T: Format&gt;</code>.  Is this an unstable feature? it gave me E0658</p>



<a name="212718303"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212718303" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212718303">(Oct 08 2020 at 16:48)</a>:</h4>
<p>can you post the full error?</p>



<a name="212719766"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212719766" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dave E <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212719766">(Oct 08 2020 at 16:58)</a>:</h4>
<p>Error:</p>
<div class="codehilite"><pre><span></span><code>error[E0658]: associated type bounds are unstable
   --&gt; src/main.rs:228:19
    |
228 | impl StdoutWriter&lt;T: Format&gt; {
    |                   ^^^^^^^^^
    |
    = note: see issue #52662 &lt;https://github.com/rust-lang/rust/issues/52662&gt; for more information

error[E0107]: wrong number of type arguments: expected 1, found 0
   --&gt; src/main.rs:228:6
    |
228 | impl StdoutWriter&lt;T: Format&gt; {
    |      ^^^^^^^^^^^^^^^^^^^^^^^ expected 1 type argument

error[E0229]: associated type bindings are not allowed here
   --&gt; src/main.rs:228:19
    |
228 | impl StdoutWriter&lt;T: Format&gt; {
    |                   ^^^^^^^^^ associated type not allowed here

error: aborting due to 3 previous errors

Some errors have detailed explanations: E0107, E0229, E0658.
</code></pre></div>


<p>Code:</p>
<div class="codehilite"><pre><span></span><code>...
struct StdoutWriter&lt;T: Format&gt; {
    formatter: T,
}
impl StdoutWriter&lt;T: Format&gt; {
    fn write(&amp;self, payload: &amp;Payload) {
        eprintln!(&quot;{}&quot;, self.formatter.format(&amp;payload));
    }
}
</code></pre></div>



<a name="212719971"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212719971" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212719971">(Oct 08 2020 at 17:00)</a>:</h4>
<p>The &lt;T: &gt; goes on <code>impl</code>, not StdoutWriter in the impl block</p>



<a name="212719999"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212719999" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212719999">(Oct 08 2020 at 17:00)</a>:</h4>
<p>Hmm the diagnostic for that is really bad</p>



<a name="212720474"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212720474" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dave E <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212720474">(Oct 08 2020 at 17:03)</a>:</h4>
<p>So this?</p>
<div class="codehilite"><pre><span></span><code>struct StdoutWriter&lt;T: Format&gt; {
    formatter: T,
}
impl StdoutWriter {
    fn write(&amp;self, payload: &amp;Payload) {
        eprintln!(&quot;{}&quot;, self.formatter.format(&amp;payload));
    }
}
</code></pre></div>


<div class="codehilite"><pre><span></span><code>error[E0107]: wrong number of type arguments: expected 1, found 0
   --&gt; src/main.rs:228:6
    |
228 | impl StdoutWriter {
    |      ^^^^^^^^^^^^ expected 1 type argument
</code></pre></div>



<a name="212721862"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/struct%20attribute%20that%20implements%20Trait/near/212721862" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/struct.20attribute.20that.20implements.20Trait.html#212721862">(Oct 08 2020 at 17:14)</a>:</h4>
<p><code>impl&lt;T: Format&gt; StdoutWriter&lt;T&gt; {</code></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>